package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.vo.CategoryVo;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Resource
    CategoryMapper categoryMapper;

    @Override
    public List<Category> treeSelect(Long id) {
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getParentId, id);
        List<Category> categories = baseMapper.selectList(categoryLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(categories)){
            //为每一个category填充是否有子结点
            //1.查找个数
            categories.forEach(category -> {
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>()
                        .eq(Category::getParentId,category.getId())
                );
                category.setHasChildren(count>0);
            }
            );
        }
        return categories;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        List<Category> categories = baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        return categories.stream().map( item->{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item,categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        List<CategoryVo> categories = categoryMapper.selectVoList();
        // Step 1: 创建 Map 以快速查找分类对象
        Map<Long, CategoryVo> categoryMap = new HashMap<>();
        for (CategoryVo categoryVo : categories) {
            categoryMap.put(categoryVo.getId(), categoryVo);
        }

        // Step 2: 遍历分类对象，填充子分类
        for (CategoryVo categoryVo : categories) {
            Long parentId = categoryVo.getParentId();
            if (parentId!=0 && categoryMap.containsKey(parentId)) {
                CategoryVo parent = categoryMap.get(parentId);
                parent.getChildren().add(categoryVo);
            }
        }
        // Step 3: 收集顶级分类（parentId = 0）
        List<CategoryVo> result = new ArrayList<>();
        for (CategoryVo categoryVo : categories) {
            if (categoryVo.getParentId() == 0) {
                result.add(categoryVo);
            }
        }
        return result;
    }
}
